package edu.odu.cs.sstore.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import edu.odu.cs.sstore.dto.CourseDTO;
import edu.odu.cs.sstore.dto.DocumentDTO;
import edu.odu.cs.sstore.dto.UserDTO;

@Entity
public class Course implements Serializable {

	private static final long serialVersionUID = 351106771219751728L;

	@Id
	private Long crn;
	private String name;
	private String code;
	
	@ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "facultyUserId")
    private User faculty;
	
	@ManyToMany(cascade = CascadeType.ALL, mappedBy="registeredCourses")
    private Set<User> students;
	
	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Set<Document> documents;
	
	public Course() { 
		this.students = new HashSet<User>();
		this.documents = new HashSet<Document>();
	}
	
	public Course(Long crn, String name, String code, User faculty) {
		setCrn(crn);
		setName(name);
		setFaculty(faculty);
		this.students = new HashSet<User>();
		this.documents = new HashSet<Document>();
	}

	public Long getCrn() {
		return crn;
	}

	public void setCrn(Long crn) {
		this.crn = crn;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public User getFaculty() {
		return faculty;
	}

	// TODO - Do a preauthorize check to ensure the user is a member of the ROLE_FACULTY
	public void setFaculty(User faculty) {
		this.faculty = faculty;
	}

	public Collection<User> getStudents() {
		return this.students;
	}
	
	public void addStudent(User student) {
		this.students.add(student);
	}
	
	public boolean hasStudent(User student) {
		return this.students.contains(student);
	}
	
	public void removeStudent(User student) {
		this.students.remove(student);
	}
	
	public void addDocument(Document document) {
		this.documents.add(document);
	}
	
	public void removeDocument(Document document) {
		this.documents.remove(document);
	}
	
	public Set<Document> getDocuments() {
		return this.documents;
	}
	
	public CourseDTO toDTO() {
		return this.toDTO(false);
	}
	
	public CourseDTO toDTO(boolean includeStudents) {
		
		Set<UserDTO> students = null;
		if(includeStudents) {
			students = new HashSet<UserDTO>();
			for(User student : this.students)
				students.add(student.toDTO());
		}
		
		Set<DocumentDTO> documents = new HashSet<DocumentDTO>();
		for(Document document : this.documents)
			documents.add(document.toDTO());
		
		return new CourseDTO(
			this.crn,
			this.code,
			this.name,
			this.faculty.toDTO(),
			students,
			documents
		);
		
	}
}
